package com.baozheyangken.ai_learn.controller;

import com.baozheyangken.ai_learn.annotation.RequiresAuthentication;
import com.baozheyangken.ai_learn.mapper.DifficultyLevelMapper;
import com.baozheyangken.ai_learn.pojo.dto.KnowledgePageQueryDTO;
import com.baozheyangken.ai_learn.pojo.entity.DifficultyLevel;
import com.baozheyangken.ai_learn.pojo.result.PageResult;
import com.baozheyangken.ai_learn.pojo.result.Result;
import com.baozheyangken.ai_learn.pojo.vo.KnowledgeInsertVO;
import com.baozheyangken.ai_learn.pojo.vo.KnowledgeUpdateVO;
import com.baozheyangken.ai_learn.service.KnowledgeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@RequestMapping("/knowledge")
@RestController
@Slf4j
@RequiresAuthentication
public class KnowledgeController {

    @Autowired
    private KnowledgeService knowledgeService;

    /**
     * 添加知识点
     *
     * @param knowledgeVO
     * @return
     */
    @PostMapping
    public Result addKnowledge(@RequestBody KnowledgeInsertVO knowledgeVO) {
        return knowledgeService.knowledgeInsert(knowledgeVO);
    }

    /**
     * 根据课程分页查询知识点
     *
     * @param knowledgePageQueryDTO
     * @return
     */
    @PostMapping("/page")
    public Result<PageResult> getKnowledge(@RequestBody KnowledgePageQueryDTO knowledgePageQueryDTO) {
        log.info("根据课程分页查询知识点{}", knowledgePageQueryDTO);
        PageResult pageResult = knowledgeService.knowledgeQuery(knowledgePageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 更新知识点
     *
     * @param knowledgeVO
     * @return
     */
    @PutMapping
    public Result updateKnowledge(@RequestBody KnowledgeUpdateVO knowledgeVO) {
        log.info("更新知识点{}", knowledgeVO);
        return knowledgeService.knowledgeUpdate(knowledgeVO);
    }

    /**
     * 删除知识点
     *
     * @param knowledgeId
     * @return
     */
    @DeleteMapping("/{knowledgeId}")
    public Result deleteKnowledge(@PathVariable Long knowledgeId) {
        log.info("删除知识：{}", knowledgeId);
        return knowledgeService.knowledgeDelete(knowledgeId);
    }

    /**
     * 导出模板
     *
     * @return
     * @throws IOException
     */
    @GetMapping("/export/template")
    public ResponseEntity<Resource> exportTemplate() throws IOException {
        Resource resource = knowledgeService.exportTemplate();
        log.info("导出知识点模板");
        return ResponseEntity.ok()
                .contentType(MediaType.parseMediaType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"))
                .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"template.xlsx\"")
                .body(resource);
    }

    /**
     * 导入知识点
     *
     * @param file
     * @return
     */
    @PostMapping("/import")
    public Result<String> importKnowledge(@RequestParam("file") MultipartFile file,
                                          @RequestParam("courseId") Long courseId) {
        log.info("向courseId为{}的课程导入知识点", courseId);

        if (file.isEmpty())
            return Result.error("请上传文件");
        if (!file.getOriginalFilename().endsWith(".xlsx"))
            return Result.error("仅支持.xlsx格式文件");

        return knowledgeService.importKnowledge(file, courseId);
    }

    /**
     * 导出知识点
     * @param courseId
     * @return
     * @throws Exception
     */
    @GetMapping("/export")
    public ResponseEntity<Resource> exportKnowledgeExcel(@Param("courseId") Long courseId) throws Exception {
        Resource resource = knowledgeService.exportExcel(courseId);
        log.info("导出课程id：{}的知识点", courseId);
        return ResponseEntity.ok()
                .contentType(MediaType.parseMediaType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"))
                .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"knowledge" + courseId + ".xlsx\"")
                .body(resource);
    }

    @Autowired
    private DifficultyLevelMapper difficultyLevelMapper;

    @GetMapping("/difficulty")
    public Result<List<DifficultyLevel>>  getDifficulty() {
        return Result.success(difficultyLevelMapper.getDifficultyIds());
    }

}